package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Classes;
import com.example.demo.entity.Schedule;
import com.example.demo.entity.Student;
import com.example.demo.entity.WxUser;
import com.example.demo.mapper.ClassMapper;
import com.example.demo.mapper.ScheduleMapper;
import com.example.demo.mapper.StuMapper;
import com.example.demo.mapper.WxUserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/schedule")
public class ScheduleController {
    @Resource
    ScheduleMapper scheduleMapper;
    @Resource
    ClassMapper classesMapper;
    @Resource
    WxUserMapper wxUserMapper;
    @Resource
    StuMapper stuMapper;



    @PostMapping
    public Result<?> save(@RequestBody Schedule schedule) {
        schedule.setTime(schedule.getStartTime() + "-" + schedule.getEndTime());
        schedule.setTitle(schedule.getTime() + " " + schedule.getTeacher());
        schedule.setStatus("可预约");
        schedule.setNum(0);
        schedule.setInNum(schedule.getNum() + "/" + schedule.getMaxNum());
        scheduleMapper.insert(schedule);
        return Result.success();

    }

    @PutMapping("/update")
    public Result<?> update(@RequestBody Schedule schedule) {
        schedule.setTime(schedule.getStartTime() + "-" + schedule.getEndTime());
        schedule.setTitle(schedule.getTime() + " " + schedule.getTeacher());
        schedule.setInNum(schedule.getNum() + "/" + schedule.getMaxNum());
        scheduleMapper.updateById(schedule);
        return Result.success();

    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        scheduleMapper.deleteById(id);
        return Result.success();

    }

    @GetMapping("/classes")
    public Result<?> classes() {

        List<Classes> classesList = classesMapper.selectList(null);
        System.out.println(classesList);
        return Result.success(classesList);

    }


    @GetMapping("/Query")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {

        System.out.println("sss");
        LambdaQueryWrapper<Schedule> wrapper = Wrappers.<Schedule>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Schedule::getId, search);
        }
        Page<Schedule> stuPage = scheduleMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(stuPage);

    }

    @PostMapping("/order")
    public int order(@RequestBody JSONObject req){
        String id = req.getStr("id");
        String nickName = req.getStr("nickName");


        QueryWrapper<WxUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nick_name",nickName);
        queryWrapper.eq("course_id",id);
        WxUser wxUser  = wxUserMapper.selectOne(queryWrapper);

        QueryWrapper<Student> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("nick_name",nickName);
        Student stu  = stuMapper.selectOne(queryWrapper2);
        System.out.println(stu.getResidue());


        if(wxUser == null){
            WxUser wxUser1 = new WxUser();
            wxUser1.setNickName(nickName);
            wxUser1.setCourseId(id);
            wxUserMapper.insert(wxUser1);

            Schedule schedule = scheduleMapper.selectById(id);
            int inNum = schedule.getNum();
            int num = inNum + 1;
            schedule.setNum(num);
            schedule.setInNum(schedule.getNum() + "/" + schedule.getMaxNum());
            scheduleMapper.updateById(schedule);


            int rest = stu.getResidue();
            if(rest > 0){
                int newRest = rest - 1;
                stu.setResidue(newRest);
                stuMapper.updateById(stu);
                return 1;
            }else {
                return 2;
            }


        }else {
            return  -1;
        }
    }

    @GetMapping("/wx")
    public Result<?> schedule() {

        List<Schedule> scheduleList = scheduleMapper.selectList(null);
//        System.out.println(scheduleList);
        return Result.success(scheduleList);

    }


    @GetMapping("/oderList")
    public Result<?> oderList() {


        String nickName = "Hana";
        QueryWrapper<WxUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nick_name",nickName);

        List<WxUser> wxUserList = wxUserMapper.selectList(queryWrapper);
        List idList = wxUserList.stream().map(WxUser::getCourseId).collect(Collectors.toList());

        List <Schedule> oderList = new ArrayList<Schedule>();
        for(Object id : idList){
            QueryWrapper<Schedule> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id",id);
            Schedule schedule  = scheduleMapper.selectOne(queryWrapper1);

            oderList.add(schedule);
        }
        return Result.success(oderList);
    }

    @PostMapping("/detail")
    public Schedule detail(@RequestBody JSONObject req) {
        String id = req.getStr("id");
        System.out.println("请求到了");
        System.out.println(id);
        Schedule schedule = scheduleMapper.selectById(id);
        System.out.println(schedule);
        System.out.println("3333333333=============");
        return schedule;
    }


    @PostMapping("/{orderDel}")
    public int orderDel(@RequestBody JSONObject req) {
        String id = req.getStr("id");
        String nickName = req.getStr("nickName");
        HashMap<String, Object> map = new HashMap<>();
        map.put("nick_name", nickName);
        map.put("course_id", id);
        int result = wxUserMapper.deleteByMap(map);
        System.out.println(result);

        QueryWrapper<Student> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("nick_name",nickName);
        Student stu  = stuMapper.selectOne(queryWrapper2);
        System.out.println(stu.getResidue());

        int rest = stu.getResidue();
        int newRest = rest + 1;
        stu.setResidue(newRest);
        stuMapper.updateById(stu);

        return 1;
    }


}
